// childPage/goodsDetail/goodsDetail.js
const request = require('../../util/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        max: 0,
        min: 0,
        skus: [],
        //默认当前单品
        curIndex: 0,
        //存储挡圈选中单品，默认为空
        selectProps: '',
        beginSelect: {
            '0': false
        },
        //选中后的信息
        selectMsg: {},
        hideModal: true, //模态框的状态  true-隐藏  false-显示
        animationData: {}, //
    },

    //选择单品
    selectProps(e) {
        console.log(e.currentTarget.dataset.index)
        let index = e.currentTarget.dataset.index
        //点击
        //初始化选中状态为未选中
        if (!selectState) {
            var selectState = {};
        }
        selectState[index] = !this.data.beginSelect[index]
        this.setData({
            beginSelect:selectState,
        })
        console.log(this.data.beginSelect)
        console.log(this.data.beginSelect[index])
        //遍历当前选中项，获取其中的price和name，赋值给当前显示
        if(this.data.beginSelect[index] == true){
            for(let key in selectState){
                let selectMsg = {}
                selectMsg.price = this.data.skus[key].salePrice
                selectMsg.props = this.data.skus[key].props
                console.log(selectMsg)
                this.setData({
                    selectMsg
                })
                console.log(this.data.selectMsg)
            }
        }else{
            this.setData({
                selectMsg:{}
            })
            console.log(this.data.selectMsg)
        }

    },



    //查看轮播图大图
    skim(e) {
        const urls = this.data.list.imgs
        console.log(urls)
        const current = e.currentTarget.dataset.index
        console.log(e.currentTarget.dataset.index)
        wx.previewImage({
            urls: 'https://obuge.com/juzao' + urls,
            current
        })
    },
    //返回按钮
    back() {
        wx.navigateBack({
            delta: 1,
        })
    },

    //根据produId获取数据
    getProduct(productId) {
        request.get('/wx/detailProduct', {
            productId: productId
        }).then(res => {
            res.imgsBanner = res.imgs
            this.setData({
                list: res,
                skus: res.skus
            })
            console.log(this.data.skus);
            let max = this.data.skus[0].salePrice
            let min = this.data.skus[0].salePrice
            //遍历sku获取最低价和最高价
            for (let i = 0; i < res.skus.length; i++) {
                if (res.skus[i].originalPrice >= max) max = res.skus[i].salePrice
                if (res.skus[i].originalPrice < min) min = res.skus[i].salePrice
            };
            console.log(max + min)
            this.setData({
                max,
                min
            })
            console.log(this.data.max + '-' + this.data.min)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options),
            this.getProduct(options.productId)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    // 显示遮罩层
    showModal: function () {
        var that = this;
        that.setData({
            hideModal: false
        })
        var animation = wx.createAnimation({
            duration: 600, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        setTimeout(function () {
            that.fadeIn(); //调用显示动画
        }, 200)
    },

    // 隐藏遮罩层
    hideModal: function () {
        var that = this;
        var animation = wx.createAnimation({
            duration: 800, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        that.fadeDown(); //调用隐藏动画   
        setTimeout(function () {
            that.setData({
                hideModal: true
            })
        }, 720) //先执行下滑动画，再隐藏模块

    },

    //动画集
    fadeIn: function () {
        this.animation.translateY(0).step()
        this.setData({
            animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
        })
    },
    fadeDown: function () {
        this.animation.translateY(300).step()
        this.setData({
            animationData: this.animation.export(),
        })
    },


})